package cn.jingyuan.bee.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;

public class FastJsonUtils {

    public static final SerializerFeature[] PRETTY_FORMAT = {
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteMapNullValue
    };

    public static final SerializerFeature[] DEFAULT_FORMAT = {
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.SortField,
            SerializerFeature.WriteMapNullValue
    };


    /**
     * 转 json 字符串
     * <blockquote>美化输出</blockquote>
     * <blockquote>日期输出格式：yyyy-MM-dd HH:mm:ss</blockquote>
     * <blockquote>String null : "key" : null</blockquote>
     * <blockquote>美化输出</blockquote>
     */
    public static String jsonPretty(Object object) {
        return JSON.toJSONString(
                object,
                BeeSerializeConfig.globalInstance,
                PRETTY_FORMAT
        );
    }

    /**
     * 转 json 字符串
     * <blockquote>日期输出格式：yyyy-MM-dd HH:mm:ss</blockquote>
     * <blockquote>枚举输出格式：枚举名称</blockquote>
     * <blockquote>String null : "key" : null</blockquote>
     */
    public static String json(Object object) {
        return JSON.toJSONString(
                object,
                BeeSerializeConfig.globalInstance,
                DEFAULT_FORMAT
        );
    }

    public static String json(Object object, List<SerializerFeature> features) {
        return json(object, features, false);
    }

    public static String json(Object object, List<SerializerFeature> features, boolean prettyFormat) {
        if (prettyFormat) {
            features.add(SerializerFeature.PrettyFormat);
        }

        return json(object, BeeSerializeConfig.getGlobalInstance(), features, prettyFormat);
    }

    public static String json(Object object, SerializeConfig config, List<SerializerFeature> features, boolean prettyFormat) {
        if (prettyFormat) {
            features.add(SerializerFeature.PrettyFormat);
        }

        SerializerFeature[] serializerFeatures = (SerializerFeature[]) features.toArray();

        return JSON.toJSONString(object, config, serializerFeatures);
    }

}
